/*
 * Copyright (c) 2022-2022 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "It_posix_container.h"
#include "sys/mount.h"

#define STACK_SIZE (1024 * 1024)    /* Stack size for cloned child */
#define CHILD_FUNC_ARG (0x2088)

#define USERDATA_DEV_NAME   "/dev/mmcblk0p2"
#define USERDATA_DIR_NAME   "/userdata"
#define ACCESS_FILE_NAME    USERDATA_DIR_NAME "/mntcontainertest"
#define FS_TYPE             "vfat"

static int childFunc(void *arg) {
    int ret;
    int value = *((int*)arg);
    ICUNIT_ASSERT_EQUAL(value, CHILD_FUNC_ARG, value);

    std::cout << "child unshare with NEW_NS" << std::endl;
    ret = unshare(CLONE_NEWNS);
    ICUNIT_ASSERT_EQUAL(ret, 0, ret);

    std::cout << "child mount(" << USERDATA_DIR_NAME << ") after unshare" << std::endl;
    ret = mount(USERDATA_DEV_NAME, USERDATA_DIR_NAME, FS_TYPE, 0, NULL);
    ICUNIT_ASSERT_EQUAL(ret, 0, ret);

    std::cout << "child access(" << ACCESS_FILE_NAME << ") after mount" << std::endl;
    ret = access(ACCESS_FILE_NAME, F_OK);
    ICUNIT_ASSERT_EQUAL(ret, 0, ret);

    return 0;
}

/* mount container unshare test: child clone without NEW_NS, unshare in child with NEW_NS, umount in child */
static UINT32 Testcase(VOID)
{
    int ret;
    char *stack;
    char *stackTop;
    stack = (char*)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
    stackTop = stack + STACK_SIZE;

    std::cout << "parent umount(" << USERDATA_DIR_NAME << ")" << std::endl;
    ret = umount(USERDATA_DIR_NAME);
    ICUNIT_ASSERT_EQUAL(ret, 0, ret);

    int arg = CHILD_FUNC_ARG;
    std::cout << "child clone from parent without NEW_NS" << std::endl;
    auto pid = clone(childFunc, stackTop, 0, &arg);
    std::cout << "child pid is " << pid << std::endl;
    int status = 0, rc = 0;
    rc = waitpid(pid, &status, 0);
    if (!WIFEXITED(status)){
        perror("waitpid");
        return 1;
    }
    ICUNIT_ASSERT_NOT_EQUAL(pid, -1, pid);

    std::cout << "parent can't access(" << ACCESS_FILE_NAME << ") after child unshare and mount" << std::endl;
    ret = access(ACCESS_FILE_NAME, F_OK);
    ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);

    std::cout << "parent can't umount(" << USERDATA_DIR_NAME << ") after child unshare and mount" << std::endl;
    ret = umount(USERDATA_DIR_NAME);
    ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);

    std::cout << "parent mount(" << USERDATA_DIR_NAME << ") after child unshare and mount" << std::endl;
    ret = mount(USERDATA_DEV_NAME, USERDATA_DIR_NAME, FS_TYPE, 0, NULL);
    ICUNIT_ASSERT_EQUAL(ret, 0, ret);

    return 0;
}

VOID ItPosixContainerMnt007(void)
{
    TEST_ADD_CASE("IT_POSIX_CONTAINER_MNT_007", Testcase, TEST_POSIX, TEST_CONTAINER, TEST_LEVEL0, TEST_FUNCTION);
}

